package com.cskaoyan.service.impl;

import com.cskaoyan.bean.ProfileLsnoticeParameter;
import com.cskaoyan.mapper.MarketNoticeAdminMapper;
import com.cskaoyan.mapper.MarketNoticeMapper;
import com.cskaoyan.model.MarketNotice;
import com.cskaoyan.model.MarketNoticeAdmin;
import com.cskaoyan.model.MarketNoticeAdminExample;
import com.cskaoyan.model.MarketNoticeExample;
import com.cskaoyan.service.ProfileService;
import com.cskaoyan.utils.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.*;
import java.util.stream.Collectors;

public class ProfileServiceImpl implements ProfileService {
    @Override
    public List<MarketNotice> lsnotice(ProfileLsnoticeParameter lsnoticeParameter) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
//        MarketNoticeAdminMapper noticeAdminMapper = sqlSession.getMapper(MarketNoticeAdminMapper.class);
        MarketNoticeMapper noticeMapper = sqlSession.getMapper(MarketNoticeMapper.class);

        //分页
        PageHelper.startPage(lsnoticeParameter.getPage(), lsnoticeParameter.getLimit());

//        MarketNoticeAdminExample noticeAdminExample = new MarketNoticeAdminExample();
//        MarketNoticeAdminExample.Criteria criteria = noticeAdminExample.createCriteria();
        String type = lsnoticeParameter.getType();
        if ("unread".equals(type)) {
//            criteria.andReadTimeIsNull();
            List<MarketNotice> marketNoticeList1 = noticeMapper.selectByParameterNoReadTime(lsnoticeParameter.getTitle(),lsnoticeParameter.getOrder(),lsnoticeParameter.getSort());
            sqlSession.close();
            return marketNoticeList1;
        }
        if ("read".equals(type)) {
//            criteria.andReadTimeIsNotNull();
            List<MarketNotice> marketNoticeList = noticeMapper.selectByParameterReadTime(lsnoticeParameter.getTitle(),lsnoticeParameter.getOrder(),lsnoticeParameter.getSort());
            sqlSession.close();
            return marketNoticeList;

        }
        List<MarketNotice> marketNoticeList = noticeMapper.selectByParameterAll(lsnoticeParameter.getTitle(),lsnoticeParameter.getOrder(),lsnoticeParameter.getSort());
//        List<MarketNoticeAdmin> noticeAdminList = noticeAdminMapper.selectByExample(noticeAdminExample);
//        Set<Integer> noticeIds = noticeAdminList.stream().map(MarketNoticeAdmin::getNoticeId).collect(Collectors.toSet());
//        MarketNoticeExample noticeExample = new MarketNoticeExample();
//        noticeExample.setOrderByClause(lsnoticeParameter.getSort() + " " + lsnoticeParameter.getOrder());
//        if (!noticeIds.isEmpty()) {
//            MarketNoticeExample.Criteria noticeExampleCriteria = noticeExample.createCriteria();
//            noticeExampleCriteria.andIdIn(noticeIds.stream().toList());
//            String title = lsnoticeParameter.getTitle();
//            if (title != null && !title.isEmpty())
//                noticeExampleCriteria.andTitleLike("%" + title + "%");
//            List<MarketNotice> noticeList = noticeMapper.selectByExample(noticeExample);
//            if (flag) {
//                for (MarketNotice marketNotice : noticeList) {
//                    marketNotice.setReadTime(new Date());
//                    marketNotice.setNoticeTitle("小组项目漏洞查找");
//                }
//            }else for (MarketNotice marketNotice : noticeList) {
//                marketNotice.setNoticeTitle("小组项目漏洞查找");
//            }
//            return noticeList;
//        }

        sqlSession.close();
        return marketNoticeList;
    }

    @Override
    public MarketNotice catnotice(MarketNotice notice) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketNoticeAdminMapper noticeAdminMapper = sqlSession.getMapper(MarketNoticeAdminMapper.class);
        MarketNoticeAdmin noticeAdmin = new MarketNoticeAdmin();
        noticeAdmin.setReadTime(new Date());
        noticeAdmin.setUpdateTime(new Date());
        MarketNoticeAdminExample noticeAdminExample = new MarketNoticeAdminExample();
        noticeAdminExample.createCriteria().andNoticeIdEqualTo(notice.getId());
        noticeAdminMapper.updateByExampleSelective(noticeAdmin, noticeAdminExample);
        sqlSession.commit();
        sqlSession.close();
        return notice;
    }
}
